package com.baihan.angus.app.game.busshow.ui.script_library;

import android.os.Bundle;
import android.widget.LinearLayout;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.baihan.angus.app.game.basic.base.BaseFragment;
import com.baihan.angus.app.game.basic.utils.Tooast;
import com.baihan.angus.app.game.busshow.BR;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.ActivityAdp;
import com.baihan.angus.app.game.busshow.adps.BannerViewHolder;
import com.baihan.angus.app.game.busshow.adps.HotRankTopAdp;
import com.baihan.angus.app.game.busshow.databinding.FragmentScriptDepotBinding;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.baihan.angus.app.game.common.bean.response.ActivityResponse;
import com.baihan.angus.app.game.common.bean.response.ScriptRankResponse;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 剧库
 */
public class ScriptDepotFragment extends BaseFragment<FragmentScriptDepotBinding, ScriptDepotVM> {

    private List<String> bannersPic = new ArrayList<>();
    private List<ActivityResponse> activityList = new ArrayList<>();
    private List<ScriptRankResponse.Data> rankList = new ArrayList<>();

    private Map<String, Object> mScriptRequest = new HashMap<>();
    private Map<String, Object> mActivityRequest = new HashMap<>();

    private ActivityAdp mActivityAdp;
    private HotRankTopAdp mHotRankTopAdp;

    private int mPage = 1;
    private int mSize = 10;
    private int totalPage = 0;

    public ScriptDepotFragment() {
        // Required empty public constructor
    }

    public static ScriptDepotFragment newInstance() {
        ScriptDepotFragment fragment = new ScriptDepotFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.scriptDepotVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_script_depot;
    }

    @Override
    protected void initView() {

        LinearLayout.LayoutParams top = (LinearLayout.LayoutParams) mVDBinding.statusBarFillView.getLayoutParams();
        top.height = ImmersionBar.getStatusBarHeight(this);
        mVDBinding.statusBarFillView.setLayoutParams(top);

        mVDBinding.llSearch.setOnClickListener(view -> {

        });

        mVDBinding.ivMore.setOnClickListener(view -> {

        });

        mVDBinding.llBookCar.setOnClickListener(view -> {
//            ARouter.getInstance().build(ArouterAppPath.GAME_ACTIVITY).navigation();
        });

        mVDBinding.llOnlineExhibition.setOnClickListener(view -> {
            ARouter.getInstance().build(ArouterAppPath.EXHIBITION_ACTIVITY).navigation();
        });

        mVDBinding.llGenuineStore.setOnClickListener(view -> {
            ARouter.getInstance().build(ArouterAppPath.STORE_LIST_ACTIVITY).navigation();
        });

        mVDBinding.llMoreActivity.setOnClickListener(view -> {
            Tooast.normalInfo("敬请期待");
        });

        mHotRankTopAdp = new HotRankTopAdp(getActivity(), rankList);
        mVDBinding.rvHotScript.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
        mHotRankTopAdp.bindRecycleView(mVDBinding.rvHotScript);

        mActivityAdp = new ActivityAdp();
        mVDBinding.rvActivity.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
        mActivityAdp.bindToRecycleView(mVDBinding.rvActivity);

        mActivityAdp.setOnItemClickListener(new ActivityAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Tooast.normalInfo("敬请期待");
            }
        });

        bannersPic.add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic_source%2F53%2F0a%2Fda%2F530adad966630fce548cd408237ff200.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640743935&t=36ac77a1689dd57ca732e41fbe2ea71e");
        bannersPic.add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp05%2F19100120461512E-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640743898&t=82426178a19093bb38a9b8be75c482f9");
        bannersPic.add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp05%2F19100220060CY5-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640743920&t=750492d4c4cbea898253492a7e637b12");
        mVDBinding.banner.setPages(bannersPic, () -> new BannerViewHolder());
        mVDBinding.banner.start();
    }

    @Override
    protected void initData() {
        ActivityResponse activityResponse1 = new ActivityResponse("合肥剧本展会", "合肥", "2021.07.15-07.31");

        ActivityResponse activityResponse2 = new ActivityResponse("南京剧本展会", "南京", "2021.09.15-09.31");

        activityList.add(activityResponse1);
        activityList.add(activityResponse2);
        mActivityAdp.addAll(activityList);

        mScriptRequest.put("type", "hot");
//        mScriptRequest.put("size", mSize);
//        mScriptRequest.put("tag", "top10");
        mScriptRequest.put("order", "hits desc");
        mViewModel.getScriptRankList(mScriptRequest);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldRankList.observe(this, new Observer<List<ScriptRankResponse.Data>>() {
            @Override
            public void onChanged(List<ScriptRankResponse.Data> data) {
                if (null != data) {
                    rankList = data;
                    mHotRankTopAdp.updateRankList(data);
                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        mVDBinding.banner.pause();//暂停轮播
    }

    @Override
    public void onResume() {
        super.onResume();
        mVDBinding.banner.start();//开始轮播
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}